require('../css/person.less')
window.utils.namichref()
//性别选择器
let showsex = document.querySelector('main #showsex')
let weuiCeillBd = document.querySelector('main #showsex .weui-cell__ft')
showsex.addEventListener('click', function () {
	weui.picker(
		[
			{
				label: '男',
				value: 0,
			},
			{
				label: '女',
				value: 1,
			},
		],
		{
			onConfirm: function (result) {
				weuiCeillBd.innerHTML = result[0].label
			},
			title: '性别',
		}
	)
})
// -----------------------------------------
//生日选择器
let showday = document.querySelector('main #showday')
let showdaytext = document.querySelector('#showdaytext')
let dayvalue = null
showday.addEventListener('click', function () {
	weui.datePicker({
		start: 1990,
		end: new Date().getFullYear(),
		onConfirm: function (result) {
			let arr = result.map(function (itme) {
				return window.utils.addZero(itme.value)
			})
			dayvalue = arr.join('-')
			showdaytext.innerHTML =
				result[0].label + result[1].label + result[2].label
		},
		title: '生日选择器',
	})
})
// -----------------------------------------
//省份和城市选择器
let showcity = document.querySelector('#showcity')
let textcity = document.querySelector('#textcity')
let showprovince = document.querySelector('#showprovince')
let textprovince = document.querySelector('#textprovince')
let removeclick = null
//用接口来获取后端的数据（省份）
//将后端传过来数据转为数组包对象形式（键值对）
window.$http.get('/address/province', function (res) {
	let province = res.data.map(function (item) {
		return {
			label: item.name,
			value: item.addressId,
		}
	})
	//给省份绑定点击事件
	showprovince.addEventListener('click', function () {
		//使用weui的方法渲染数据
		weui.picker(province, {
			onConfirm: function (result) {
				textprovince.innerHTML = result[0].label
				//使用接口来获取后端传来的数据，并使用
				window.$http.get(
					`/address/city/${result[0].value}`,
					function (res) {
						let city = res.data.map(function (item) {
							return {
								label: item.name,
								value: item.addressId,
							}
						})
						showcity.removeEventListener('click', removeclick)
						removeclick = cityclick
						showcity.addEventListener('click', cityclick)
						function cityclick() {
							weui.picker(city, {
								onConfirm: function (result) {
									console.log(result)
									textcity.innerHTML = result[0].label
								},
								title: '城市选择器',
							})
						}
					}
				)
			},
			title: '省份选择器',
		})
	})
})
// -----------------------------------------
//显示输入文字的个数
let textareaNum = document.querySelector('.weui-textarea-num')
let textarea = document.querySelector('.weui-textarea')
//绑定input事件,实时刷新
textarea.addEventListener('input', function () {
	let textValue = textarea.value
	textareaNum.textContent = textValue.length
})
// -----------------------------------------
//获取本地存储中的数据
let userID = JSON.parse(localStorage.getItem('users'))
let userPrese = document.querySelector('#userPrese')
let jsInput = document.querySelector('#js_input')
userPrese.addEventListener('click', function () {
	console.log(jsInput.value)
	let userData = {
		userId: userID.userId, //
		nickname: jsInput.value, //帐户
		gender: weuiCeillBd.textContent, //性别
		birthday: dayvalue, //生日
		address: [`${textprovince.textContent}`, `${textcity.textContent}`], //地址
		describe: textarea.value, //描述
	}
	// 使用接口上传数据
	window.$http.post('/users/userEdit', userData, function (res) {
		console.log(res)
		if (res.status === 0) {
			location.href = './my.html'
			console.log(res)
		}
	})
})

// -----------------------------------------
// 网页加载时渲染
window.onload = Render
function Render() {
	window.$http.get(
		`/users/accountinfo?userId=${userID.userId}`,
		function (res) {
			console.log(res)
			weuiCeillBd.textContent = res.data.gender
			jsInput.value = res.data.account
			showdaytext.textContent = showdays(res.data.birthday)
			let arrs = res.data.address.split(',')
			textprovince.textContent = arrs[0]
			textcity.textContent = arrs[1]
			textarea.value = res.data.describe
		}
	)
}
//生日时间转换
function showdays(birthday) {
    if(birthday){
	let arr = birthday.split('-')
	let arrs = arr[2].slice(0, 2)
	arr.splice(2, 1, arrs)
	birthday = arr[0] + '年' + arr[1] + '月' + arr[2] + '日'
	return birthday
}
}
// -----------------------------------------
//点击返回我的
let myreturn = document.querySelector('#myreturn')
myreturn.addEventListener('click',function(){
    location.href='./my.html'    
})